package com.titleevaluation.controller;

import com.titleevaluation.entity.Patent;
import com.titleevaluation.service.PatentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/patents")
public class PatentController {

    private final PatentService patentService;

    public PatentController(PatentService patentService) {
        this.patentService = patentService;
    }

    @PostMapping
    public ResponseEntity<Patent> createPatent(@PathVariable Long applicationId, @RequestBody Patent patent) {
        Patent createdPatent = patentService.createPatent(applicationId, patent);
        return ResponseEntity.ok(createdPatent);
    }

    @GetMapping
    public ResponseEntity<List<Patent>> getPatentsByApplicationId(@PathVariable Long applicationId) {
        List<Patent> patents = patentService.getPatentsByApplicationId(applicationId);
        return ResponseEntity.ok(patents);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Patent> getPatentById(@PathVariable Long id) {
        return patentService.getPatentById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<Patent> updatePatent(@PathVariable Long id, @RequestBody Patent patent) {
        Patent updatedPatent = patentService.updatePatent(id, patent);
        return ResponseEntity.ok(updatedPatent);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePatent(@PathVariable Long id) {
        patentService.deletePatent(id);
        return ResponseEntity.noContent().build();
    }
}

